<?php

namespace app\models\Child;

use Yii;

/**
 * This is the model class for table "t_user_info".
 *
 * @property integer $user_id
 * @property string $open_id
 * @property string $user_name
 * @property string $user_phone
 * @property string $phone_code
 * @property string $user_img
 * @property string $user_account
 * @property integer $user_level
 * @property string $user_birthday
 * @property integer $user_gender
 * @property string $user_from
 * @property string $user_address
 * @property string $create_time
 *
 * @property AccountUserInfo[] $accountUserInfos
 * @property TBookingInfo[] $tBookingInfos
 * @property TPatientInfo[] $tPatientInfos
 * @property TReplyInfo[] $tReplyInfos
 * @property TUserCardConsumerRel[] $tUserCardConsumerRels
 * @property InitGradeUser $userLevel
 */
class TUserInfo extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 't_user_info';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_account'], 'number'],
            [['user_level', 'user_gender'], 'integer'],
            [['create_time'], 'safe'],
            [['open_id', 'user_name'], 'string', 'max' => 50],
            [['user_phone'], 'string', 'max' => 11],
            [['phone_code'], 'string', 'max' => 4],
            [['user_img', 'user_address','width','height'], 'string', 'max' => 200],
            [['user_birthday'], 'string', 'max' => 10],
            [['user_from'], 'string', 'max' => 22],
            [['user_level'], 'exist', 'skipOnError' => true, 'targetClass' => InitGradeUser::className(), 'targetAttribute' => ['user_level' => 'grade_id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'user_id' => 'User ID',
            'open_id' => 'Open ID',
            'user_name' => 'User Name',
            'user_phone' => 'User Phone',
            'phone_code' => 'Phone Code',
            'user_img' => 'User Img',
            'user_account' => 'User Account',
            'user_level' => 'User Level',
            'user_birthday' => 'User Birthday',
            'user_gender' => 'User Gender',
            'user_from' => 'User From',
            'user_address' => 'User Address',
            'create_time' => 'Create Time',
            'width' => 'Width',
            'height' => 'Height',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAccountUserInfos()
    {
        return $this->hasMany(AccountUserInfo::className(), ['user_id' => 'user_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTBookingInfos()
    {
        return $this->hasMany(TBookingInfo::className(), ['user_id' => 'user_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTPatientInfos()
    {
        return $this->hasMany(TPatientInfo::className(), ['user_id' => 'user_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTReplyInfos()
    {
        return $this->hasMany(TReplyInfo::className(), ['user_id' => 'user_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTUserCardConsumerRels()
    {
        return $this->hasMany(TUserCardConsumerRel::className(), ['user_id' => 'user_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUserLevel()
    {
        return $this->hasOne(InitGradeUser::className(), ['grade_id' => 'user_level']);
    }
    
    /*
     * 设置会员资料
     */
    public function setUserInfo($user_id,$user_img,$user_name,$user_gender,$user_birthday,$width,$height)
    {
        $user = $this->find()
                ->where(['user_id' => $user_id])
                ->one();
        if(!empty($user_img))
        {
            $user->user_img = $user_img;
        }
        $user->user_name = $user_name;
        if(!empty($user_gender))
        {
            $user->user_gender = $user_gender;
        }
        if(!empty($user_birthday))
        {
            $user->user_birthday = $user_birthday;
        }
        $user->width = $width;
        $user->height = $height;
        
        return $user->save();
    }
    
    /*
     * 获取会员资料
     */
    public function getUserList($user_id)
    {
        $list = $this->find()
                ->select('t_user_info.*,grade_name,grade_img')
                ->join('left join','init_grade_user','init_grade_user.grade_id = t_user_info.user_level')
                ->where(['user_id' => $user_id])
                ->asArray()
                ->one();
        return $list;
    }
    
    /*
     * 根据openid查询用户是否存在
     */
    public function getUser($openid)
    {
        $list = $this->find()
                ->where(['open_id' => $openid])
                ->asArray()
                ->one();
        return $list;
    }
    
    /*
     * 获取用户信息
     */
    public function getOpenId($user_id)
    {
        $list = $this->find()
                ->where(['user_id' => $user_id])
                ->asArray()
                ->one();
        
        return $list;
    }

    //通过用户ID修改用户信息
    public function updateUser($user_id,$subclinic_id){
              $list1 = $this->find()
                    ->where(['user_id' => $user_id])
                    ->one();
              $list1->subclinic_id = $subclinic_id;
              $res = $list1->save();
              return $res;
    }
    
    /*
     * 根据手机号获取用户id
     */
    public function getList($user_phone,$open_id,$user_name)
    {
        
        $list = $this->find()
                ->where(['user_phone' => $user_phone])
                ->asArray()
                ->one();
        return $list;
    }
    
}
